/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml;

import dto.Application;
import dto.BlockCondition;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import util.DateUtil;

/**
 *
 * @author phat10130059
 */
public class XmlFile5 {

    public static String readFileXml(String file) {

        String tmp = "";

        try {

            File fXmlFile = new File(file);

            if (fXmlFile.exists() && !fXmlFile.isDirectory()) {

                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(fXmlFile);

                //optional, but recommended
                //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
                doc.getDocumentElement().normalize();

                System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

                Node node = doc.getElementsByTagName("start").item(0);

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) node;
                    tmp = eElement.getTextContent();

                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return tmp;
    }

    public static void createXmlNew(String filePath, BlockCondition blockCondition) {
        try {

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            // root elements
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("Website");
            doc.appendChild(rootElement);

            // staff elements
            Element block = doc.createElement("start");
            block.appendChild(doc.createTextNode(blockCondition.getBlock()));
            rootElement.appendChild(block);

            // write the content into xml filePath
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(filePath));

            // Output to console for testing
            // StreamResult result = new StreamResult(System.out);

            transformer.transform(source, result);

            System.out.println("File saved!");

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (TransformerException tfe) {
            tfe.printStackTrace();
        }
    }

    public static void editXml(String file, BlockCondition blockCondition) {
        try {

            File fXmlFile = new File(file);

            if (fXmlFile.exists() && !fXmlFile.isDirectory()) {
                
                String filepath = file;
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                Document doc = docBuilder.parse(filepath);

                // Get the root element
                Node computer = doc.getFirstChild();
                // Get the staff element , it may not working if tag has spaces, or
                // whatever weird characters in front...it's better to use
                // getElementsByTagName() to get it directly.
                // Node staff = company.getFirstChild();
                // Get the staff element by tag name directly
                Node node = doc.getElementsByTagName("start").item(0);
                node.setTextContent(blockCondition.getBlock());


                // write the content into xml file
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(doc);
                StreamResult result = new StreamResult(new File(filepath));
                transformer.transform(source, result);

                System.out.println("Done Edit");

            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public static void main(String argv[]) {
//        String readFileXml = readFileXml("fileCondition.xml");
//        System.out.println(readFileXml);
//
//
//        BlockCondition blockCondition = new BlockCondition();
//        blockCondition.setBlock("false");
////        
////        createXmlNew("fileCondition.xml", blockCondition);
//        
//        
////        editXml("fileCondition.xml", blockCondition);
//
//    }
}
